<template>
  <!-- 添加用户对话框 -->
  <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="addFormClose">
    <el-form
      :model="addForm"
      :rules="addFormRules"
      ref="addFormRef"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="addForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="addForm.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="addForm.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="addForm.mobile"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="addDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addUser">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: {
    //  弹出框是否显示，其实就是当前的组件是否显示，只不过这里是用父组件进行控制的
    visible: {
      type: Boolean
    }
  },
  data() {
    // 邮箱的验证方法
    var checkEmail = (rule, value, callback) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/
      if (regEmail.test(value)) {
        return callback()
      }
      callback(new Error('请输入合法的邮箱'))
    }
    // 手机号验证方法
    var checkMobile = (rule, value, callback) => {
      const regMobile = /^1[34578]\d{9}$/
      if (regMobile.test(value)) {
        return callback()
      }
      callback(new Error('请输入合法的手机号'))
    }
    return {
      addDialogVisible: false,
      // 添加用户的表单数据
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 定义表单校验规则
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '请输入3~10位的用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '请输入6~15位密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击添加用户
    async addUser() {
      // 预校验
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return false
        // 校验通过，发起添加用户的请求
        const { data: res } = await this.$http.post('users', this.addForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加用户失败')
        }
        this.$message.success('添加用户成功')
        // 隐藏添加用户的对话框
        this.addDialogVisible = false
        // this.getUserList()
        // 当前页面没有getuserlist方法，父级有，因此用$emit方法向父级发送请求，让父级来完成这个操作
        this.$emit('addUserForForm')
      })
    },
    // 关闭添加用户的对话框后重置表单
    addFormClose() {
      this.$refs.addFormRef.resetFields()
    }
  },
  watch: {
    // 监听从父级传过来的visible属性传给addDialogVisible
    visible: function(val) {
      this.addDialogVisible = val
    },
    // 监听 当前页面的 dialog 的变化再传回给父级，否则父级一直是true
    addDialogVisible: function(val) {
      //   与async搭配使用 users使用:visible.async
      this.$emit('update:visible', val)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
